package com.shop2cn.iapi.pretrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.delivery.parameter.DeliveryInfo;
import com.shop2cn.iapi.pretrade.paremeter.SqDeliveryReq;
import com.shop2cn.iapi.pretrade.service.SqDeliveryCall;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 自动确认收货提醒通知FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqDelivery {

	private static SqDeliveryReq sqDeliveryReq = new SqDeliveryReq();
	private static SqDeliveryCall sqDeliveryCall = new SqDeliveryCall();
	
	private static OrderWapper orderWapper = new OrderWapper();
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神器订单发货");
	}

	@Before
	public void caseUp() {

		sqDeliveryReq = new SqDeliveryReq();
		sqDeliveryCall = new SqDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqDeliveryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(Long orderId, List<DeliveryInfo> deliveryInfos) {
		
		Logger.comment("校验order表");
		
		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
		
		Logger.verifyEquals(3, orderPo.getOrderStatus(), "验证orderStatus");
		
		Logger.comment("校验delivery表");
		
		for (int i = 0; i < deliveryInfos.size(); i++) {
			
			DeliveryInfo deliveryInfo = deliveryInfos.get(i);
			
			DeliveryPo deliveryPo = orderWapper
					.selectDeliveryByOrderIdAndBillCode(
							deliveryInfo.getOrderId(), deliveryInfo.getBillNo());
			
			Logger.verifyEquals(deliveryInfo.getBillNo(),
					deliveryPo.getBillNo(), "验证BillNo");
			Logger.verifyEquals(deliveryInfo.getYmtExpressCode(),
					deliveryPo.getYmtExpressCode(), "验证YmtExpressCode");
			Logger.verifyEquals(deliveryInfo.getDeliverySource(),
					deliveryPo.getSource(), "验证Source");
			Logger.verifyEquals(-1, deliveryPo.getPkgIndex(), "验证PkgIndex");
			Logger.verifyEquals(-1, deliveryPo.getSegmentIndex(),
					"验证SegmentIndex");
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqDelivery_001() {
		Logger.start(true, "订单接单后，货头发货，一个物流单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String DeliverSummary = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo = new DeliveryInfo();
			
			deliveryInfo.setOrderId(orderId);
			deliveryInfo.setBillNo(DeliverSummary);
			deliveryInfo.setYmtExpressCode("Y022");
			deliveryInfo.setExpressName("自动化测试物流");
			deliveryInfo.setDeliverySource(11);
			deliveryInfo.setDeliverySection(1);
			
			sqDeliveryReq.setDeliveryInfos(Arrays.asList(deliveryInfo));

			// 订单ID
			sqDeliveryReq.setCdId(1234L);
			sqDeliveryReq.setGoodsOwnerId(sellerId);

			sqDeliveryCall.setData(sqDeliveryReq);
			sqDeliveryCall.callService();

			Logger.verifyEquals(true, sqDeliveryCall.getSuccess(),
					"验证Success");
			
			Thread.sleep(5000);
			
			this.verifyDB(orderId, sqDeliveryReq.getDeliveryInfos());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqDelivery_002() {
		Logger.start(true, "订单接单后，货头发货，多个物流单");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String deliverSummary1 = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo1 = new DeliveryInfo();
			
			deliveryInfo1.setOrderId(orderId);
			deliveryInfo1.setBillNo(deliverSummary1);
			deliveryInfo1.setYmtExpressCode("Y022");
			deliveryInfo1.setExpressName("自动化测试物流");
			deliveryInfo1.setDeliverySource(11);
			deliveryInfo1.setDeliverySection(1);
			
			String deliverSummary2 = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo2 = new DeliveryInfo();
			
			deliveryInfo2.setOrderId(orderId);
			deliveryInfo2.setBillNo(deliverSummary2);
			deliveryInfo2.setYmtExpressCode("Y022");
			deliveryInfo2.setExpressName("自动化测试物流");
			deliveryInfo2.setDeliverySource(11);
			deliveryInfo2.setDeliverySection(1);
			
			sqDeliveryReq.setDeliveryInfos(Arrays.asList(deliveryInfo1, deliveryInfo2));

			// 订单ID
			sqDeliveryReq.setCdId(1234L);
			sqDeliveryReq.setGoodsOwnerId(sellerId);

			sqDeliveryCall.setData(sqDeliveryReq);
			sqDeliveryCall.callService();
			
			Thread.sleep(5000);

			Logger.verifyEquals(true, sqDeliveryCall.getSuccess(),
					"验证Success");
			
			this.verifyDB(orderId, sqDeliveryReq.getDeliveryInfos());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqDelivery_003() {
		Logger.start(true, "订单接单后，货头发货，一个订单发货多次");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			// 发货
			String oldBillNo = SqTradingCallService.delivery(
					autotest_goodsOwnerId, orderId, null, null);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String DeliverSummary = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo = new DeliveryInfo();
			
			deliveryInfo.setOrderId(orderId);
			deliveryInfo.setBillNo(DeliverSummary);
			deliveryInfo.setYmtExpressCode("Y022");
			deliveryInfo.setExpressName("自动化测试物流");
			deliveryInfo.setDeliverySource(11);
			deliveryInfo.setDeliverySection(1);
			
			sqDeliveryReq.setDeliveryInfos(Arrays.asList(deliveryInfo));

			// 订单ID
			sqDeliveryReq.setCdId(1234L);
			sqDeliveryReq.setGoodsOwnerId(sellerId);

			sqDeliveryCall.setData(sqDeliveryReq);
			sqDeliveryCall.callService();
			
			Thread.sleep(5000);

			Logger.verifyEquals(true, sqDeliveryCall.getSuccess(), "验证Success");
			Logger.verifyNotNull(
					orderWapper.selectActualDeleteDeliveryByBillNo(oldBillNo),
					"验证旧运单号删除成功");
			
			this.verifyDB(orderId, sqDeliveryReq.getDeliveryInfos());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqDelivery_004() {
		Logger.start(true, "订单接单后，货头发货，部分发货后，再次完整发货");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, 1);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String DeliverSummary = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo = new DeliveryInfo();
			
			deliveryInfo.setOrderId(orderId);
			deliveryInfo.setBillNo(DeliverSummary);
			deliveryInfo.setYmtExpressCode("Y022");
			deliveryInfo.setExpressName("自动化测试物流");
			deliveryInfo.setDeliverySource(11);
			deliveryInfo.setDeliverySection(1);
			deliveryInfo.setPartialDeliveryStatus(2);
			
			sqDeliveryReq.setDeliveryInfos(Arrays.asList(deliveryInfo));

			// 订单ID
			sqDeliveryReq.setCdId(1234L);
			sqDeliveryReq.setGoodsOwnerId(sellerId);

			sqDeliveryCall.setData(sqDeliveryReq);
			sqDeliveryCall.callService();
			
			Thread.sleep(5000);

			Logger.verifyEquals(true, sqDeliveryCall.getSuccess(), "验证Success");
			
			this.verifyDB(orderId, sqDeliveryReq.getDeliveryInfos());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqDelivery_005() {
		Logger.start(true, "订单接单后，货头发货，多个物流单，物流单号重复， 发货失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO);

			long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					(int)userId, BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
					
			// 物流单号
			String deliverSummary1 = TestDataManager.randomStr(10);
			
			DeliveryInfo deliveryInfo1 = new DeliveryInfo();
			
			deliveryInfo1.setOrderId(orderId);
			deliveryInfo1.setBillNo(deliverSummary1);
			deliveryInfo1.setYmtExpressCode("Y022");
			deliveryInfo1.setExpressName("自动化测试物流");
			deliveryInfo1.setDeliverySource(11);
			deliveryInfo1.setDeliverySection(1);
			
			DeliveryInfo deliveryInfo2 = new DeliveryInfo();
			
			deliveryInfo2.setOrderId(orderId);
			deliveryInfo2.setBillNo(deliverSummary1);
			deliveryInfo2.setYmtExpressCode("Y022");
			deliveryInfo2.setExpressName("自动化测试物流");
			deliveryInfo2.setDeliverySource(11);
			deliveryInfo2.setDeliverySection(1);
			
			sqDeliveryReq.setDeliveryInfos(Arrays.asList(deliveryInfo1, deliveryInfo2));

			// 订单ID
			sqDeliveryReq.setCdId(1234L);
			sqDeliveryReq.setGoodsOwnerId(sellerId);

			sqDeliveryCall.setData(sqDeliveryReq);
			sqDeliveryCall.callService();
			
			Thread.sleep(5000);

			Logger.verifyEquals(true, sqDeliveryCall.getSuccess(), "验证Success");
			Logger.verifyEquals(true,
					sqDeliveryCall.getMsg().contains("面单号重复"), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
